/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.BusinessNewsReply;
import com.boxin.ims.modules.wechat.service.BusinessNewsReplyService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 业务消息Controller
 * @author Jakemanse
 * @version 2014-01-13
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/businessNewsReply")
public class BusinessNewsReplyController extends BaseController {

	@Autowired
	private BusinessNewsReplyService businessNewsReplyService;
	
	@ModelAttribute
	public BusinessNewsReply get(@RequestParam(required=false) Long id) {
		if (id != null){
			return businessNewsReplyService.get(id);
		}else{
			return new BusinessNewsReply();
		}
	}
	
	@RequiresPermissions("wechat:businessNewsReply:view")
	@RequestMapping(value = {"list", ""})
	public String list(BusinessNewsReply businessNewsReply, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			businessNewsReply.setUser(user);
		}
        Page<BusinessNewsReply> page = businessNewsReplyService.find(new Page<BusinessNewsReply>(request, response), businessNewsReply); 
        model.addAttribute("page", page);
		return "modules/wechat/businessNewsReplyList";
	}
	

	@RequiresPermissions("wechat:businessNewsReply:view")
	@RequestMapping(value = "add")
	public String add(BusinessNewsReply businessNewsReply, Model model,RedirectAttributes redirectAttributes) {
	
		User user = UserUtils.getUser();
		if (businessNewsReply.getUser() == null || businessNewsReply.getUser().getId() <1){
			businessNewsReply.setUser(user);
		}
		
		List<BusinessNewsReply> busiNewsList = businessNewsReplyService.findByTypeSourceId(businessNewsReply.getType(),user.getId(), businessNewsReply.getSourceId());
		model.addAttribute("businessNewsReply", businessNewsReply);
		model.addAttribute("busiNewsList", busiNewsList);
		businessNewsReply.setSort((busiNewsList == null)?0:busiNewsList.size());
		
		if(busiNewsList!=null && busiNewsList.size()>9){
			addMessage(redirectAttributes, "已经超过最多图文条数.");
			model.addAttribute("addFlag", "0");
		}else{
			model.addAttribute("addFlag", "1");
		}
		
		return "modules/wechat/businessNewsReplyAdd";
	}
	
	
	@RequiresPermissions("wechat:businessNewsReply:view")
	@RequestMapping(value = "form")
	public String form(BusinessNewsReply businessNewsReply, Model model) {
	
		User user = UserUtils.getUser();
		if (businessNewsReply.getUser() == null || businessNewsReply.getUser().getId() <1){
			businessNewsReply.setUser(user);
		}
		
		
		
		model.addAttribute("businessNewsReply", businessNewsReply);
		return "modules/wechat/businessNewsReplyForm";
	}

	@RequiresPermissions("wechat:businessNewsReply:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("picfile") MultipartFile picfile,BusinessNewsReply businessNewsReply, HttpServletRequest request, HttpServletResponse response,Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, businessNewsReply)){
			return form(businessNewsReply, model);
		}
		
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String businessPath = WechatUtils.getWechatUserBusinessResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(businessPath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_business_"+System.currentTimeMillis()+fileType;
			File file = new File(businessPath+fname);
			System.out.println("保存文件:"+businessPath+fname);
			try {
				picfile.transferTo(file);
				businessNewsReply.setFilePath(fname);
			} catch (Exception e) {
				System.out.println("上传文件失败:"+businessPath+fname);
				e.printStackTrace();
			}
			
		}
		
		if(businessNewsReply.getType().contains(BusinessNewsReply.MSG_TYPE_MEMBER)
				||businessNewsReply.getType().contains(BusinessNewsReply.MSG_TYPE_SECKILL) 
				||businessNewsReply.getType().contains(BusinessNewsReply.MSG_TYPE_SHARE)
				||businessNewsReply.getType().contains(BusinessNewsReply.MSG_TYPE_COUPON)){
			businessNewsReply.setSourceId(UserUtils.getUser().getId()+"");
		}
		
		
		businessNewsReplyService.save(businessNewsReply);
		addMessage(redirectAttributes, "保存业务消息成功");
		
		return "redirect:"+Global.ADMIN_PATH+"/wechat/businessNewsReply/add?type="+businessNewsReply.getType();
		
		//return "redirect:"+Global.ADMIN_PATH+"/wechat/businessNewsReply/?repage";
	}
	
	@RequiresPermissions("wechat:businessNewsReply:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		businessNewsReplyService.delete(id);
		addMessage(redirectAttributes, "删除业务消息成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/businessNewsReply/?repage";
	}
	
	
	
	@RequestMapping(value = "ajaxDelete")
	public String ajaxDelete(Long id,HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		businessNewsReplyService.delete(id);
		Map<String,String > map = new HashMap<String,String>();
		map.put("success", "true");
		writeJson(request, response, map);
		return null;
	}
	
	
	

}
